import re

import pandas as pd
from presentation.Apriori import Apriori, Rule_generate
from presentation.load_Groceries import load_Groceries


filename = r'D:\数据仓库挖掘presentation\关联分析\Groceries数据集\Groceries.csv'
Groceries = load_Groceries(filename)
#Groceries

'''数据预处理'''
#将所有的元素放在一个列表里，扁平化
good = Groceries[0]
for i in range(1,len(Groceries)):
    for j in range(len(Groceries[i])):
        good.append(Groceries[i][j])
#用集合的形式将重复元素去掉
goodlist = set(good)
#goodlist

#按顺序得到商品编号
goodnum = [k for k in range(len(goodlist))]

#将Groceries数据集中的商品名称换成商品标号
dataset = []
for i in range(len(Groceries)):
    #list形式不能使用replace函数,Series和str才行
    data = pd.Series(Groceries[i])
    data.replace(list(goodlist),goodnum,inplace=True)
    dataset.append(list(data))
#dataset

#设定参数值
data = pd.Series(Groceries[0])
#计算数据中不同元素以及其出现的次数
data_count = data.value_counts()
for k,v in dict(data_count).items():
    #找出所有support>=0.1的商品
    if v >= len(Groceries)*0.1:
        print(k)

'''得到最大频繁项目集'''
#设置minsupport = 0.03
minsupport =  0.03
Lmaxs = Apriori(dataset,minsupport)
print(Lmaxs)

'''得到关联规则'''
#设置最小置信度
minconf = 0.25
rules = Rule_generate(dataset,Lmaxs,minconf)
print(rules)


